import org.web3j.crypto.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * Created by Administrator on 2018/5/21.
 */
public class Web3jTest {


    public static void main(String[] args) throws Exception {

        Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/uXDUNwlNOcx0UJCWjzNr"));
        Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
        String clientVersion = web3ClientVersion.getWeb3ClientVersion();
        System.out.println(clientVersion);

        String filePath = "D:/test/wallet";
//        String fileName = WalletUtils.generateNewWalletFile("123456", new File(filePath), false);
        String fileName = "UTC--2018-07-10T15-43-57.689000000Z--857267dbeae340d2da93da3fba4fe8f1dfbc75c0.json";
        String path = filePath + "/" + fileName;
        Credentials ALICE = WalletUtils.loadCredentials("123456", path);
        System.out.println(fileName);//保存你的加密文件信息
        System.out.println(ALICE.getAddress());//钱包地址
        System.out.println(ALICE.getEcKeyPair().getPrivateKey());//私钥
        System.out.println(ALICE.getEcKeyPair().getPublicKey());//公钥

        BigInteger balance = web3.ethGetBalance(ALICE.getAddress(), DefaultBlockParameterName.LATEST).send().getBalance();
        System.out.println(balance);

//        BigInteger nonce = getNonce(web3, ALICE.getAddress());

//        RawTransaction rawTransaction = createEtherTransaction(nonce, "mubia钱包地址");
//        RawTransaction
        System.out.println("done");
    }

    private static BigInteger getNonce(Web3j web3, String address) throws Exception {
        EthGetTransactionCount ethGetTransactionCount =
                web3.ethGetTransactionCount(address,DefaultBlockParameterName.LATEST).sendAsync().get();
        return ethGetTransactionCount.getTransactionCount();
    }


//    private static RawTransaction createEtherTransaction(BigInteger nonce, String toAddress) {
//        BigInteger value = Convert.toWei("数量", Convert.Unit.ETHER).toBigInteger();
//        return RawTransaction.createEtherTransaction(nonce, ParamConvert.,limit , toAddress, value);
//    }
}
